import React, { Component } from "react";
import { Card, Form, Input, Button, Checkbox, message } from "antd";
import "./index.scss";
import logo from "assets/images/logo.png";
import { login } from "api/user";
import { setToken } from "utils/storage";
export default class Login extends Component {
  state = {
    loading: false, // 加载状态
  };
  render() {
    return (
      <div className="login">
        <Card className="login-container">
          <img className="login-logo" width={200} src={logo} />
          {/* 表单 */}
          <Form
            size="large"
            onFinish={this.onFinish}
            initialValues={{
              mobile: "13911111111",
              code: "246810",
              agree: true,
            }}
          >
            <Form.Item
              name="mobile"
              validateTrigger={["onChange", "onBlur"]}
              rules={[
                {
                  required: true,
                  message: "手机号不能为空",
                  validateTrigger: "onBlur",
                },
                {
                  pattern: /^1[3-9]\d{9}$/,
                  message: "手机号格式错误",
                  validateTrigger: "onBlur",
                },
              ]}
            >
              <Input placeholder="请输入你的手机号" />
            </Form.Item>

            <Form.Item
              name="code"
              rules={[
                {
                  required: true,
                  message: "验证码不能为空",
                },
                {
                  pattern: /^\d{6}$/,
                  message: "验证码格式错误",
                },
              ]}
            >
              <Input placeholder="请输入验证码" />
            </Form.Item>

            <Form.Item
              valuePropName="checked"
              name="agree"
              rules={[
                {
                  validator(rule, value) {
                    if (value) {
                      return Promise.resolve();
                    } else {
                      return Promise.reject(new Error("请阅读并同意用户协议"));
                    }
                  },
                },
              ]}
            >
              <Checkbox>我已阅读并同意[协议]和[条款]</Checkbox>
            </Form.Item>

            <Form.Item>
              <Button
                type="primary"
                htmlType="submit"
                block
                loading={this.state.loading}
              >
                登录
              </Button>
            </Form.Item>
          </Form>
        </Card>
      </div>
    );
  }
  onFinish = async ({ mobile, code }) => {
    this.setState({
      loading: true,
    });
    try {
      const res = await login(mobile, code);
      console.log("res---login: ", res);
      //登录成功
      // // 1.保存token
      setToken(res.data.token);
      // localStorage.setItem("token", res.data.token);
      // 2. 跳转到首页?需要判断从哪来来然后跳转到哪里去  判断location.state是否有值
      const { state } = this.props.location;
      if (state) {
        this.props.history.push(state.from);
      } else {
        this.props.history.push("/home");
      }
      message.success("登录成功", 1);
    } catch (err) {
      message.warning(err.response.data.message, 1, () => {
        this.setState({
          loading: false,
        });
      });
    }
  };
}
